--
-- Author: ping.wang
-- Date: 2019-06-26 11:52:17
--
-- RewardHeroTreasureRender
--

local ClassRef = app.mvc.createListViewCell(...)
local IconRender = require("app.widget.renders.IconRender")
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardHeroTreasureRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._silverPanel = self._ui.Panel_b
    self._goldenPanel = self._ui.Panel_j

    self._silverAni = self._silverPanel:findChild("tishi"):getTimeline()
    self._goldenAni = self._goldenPanel:findChild("tishi"):getTimeline()
    self._silverItemIcon = IconRender.create(self._silverPanel:findChild("icon"))
    self._goldenItemIcon = IconRender.create(self._goldenPanel:findChild("icon"))

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data

        --dump(data)
        local lv = data.golden.needLevel

        self._ui.lv:setString(string.format(L("lua_code_text_291"),data.golden.needLevel))
        self:_initItemPanel(data, lv <= data.curLv, false, lv)
        self:_initItemPanel(data, lv <= data.curLv, true, lv)

    else
        self:setVisible( false )
    end
end


function ClassRef:_initItemPanel(data, canGet ,isGolden, needLv)
    local info
    local ani
    local panel
    local itemIcon
    if isGolden then
        info = data.golden
        ani = self._goldenAni
        panel = self._goldenPanel
        itemIcon = self._goldenItemIcon
    else
        info = data.silver
        ani = self._silverAni
        panel = self._silverPanel
        itemIcon = self._silverItemIcon
    end
    local hasBuyGolden = data.hasBuyGolden --是否买了黄金宝藏

    panel:findChild("mask"):setVisible(not canGet)
    itemIcon:setSelected(false)
    itemIcon:setClickHandler(nil)
    if info.isGet then
        panel:findChild("icon_over"):setVisible(true)
        panel:findChild("tishi"):setVisible(false)
        panel:findChild("tips"):setVisible(false)
        ani:gotoFrameAndPause(0)
        itemIcon:setTipEnable(true)
        itemIcon:setClickHandler(nil)
    else
        panel:findChild("icon_over"):setVisible(false)
        if not canGet then
            panel:findChild("tishi"):setVisible(false)
            panel:findChild("tips"):setVisible(false)
            ani:gotoFrameAndPause(0)
            itemIcon:setTipEnable(true)
            itemIcon:setClickHandler(nil)
        else
            panel:findChild("tishi"):setVisible(true)
            panel:findChild("tips"):setVisible(true)
            --显示可以领取的动画
            ani:gotoFrameAndPlay(0,true)
            itemIcon:setTipEnable(false)
            if isGolden and not hasBuyGolden then
                itemIcon:setTipEnable(true)
                itemIcon:setClickHandler(nil)
            else
                itemIcon:setClickHandler(function(render)
                    if not info.isGet then
                        self:dispatchCellEvent("heroTreasureStartGetAward", {lv=needLv,isGolden=isGolden})
                    end
                end)
            end
        end
    end

    if #info.rewards >= 2 then
        --显示一个合图
        panel:findChild("name"):setVisible(true)
        panel:findChild("name"):setString(L("battlepass_string4"))
        itemIcon:setVisible(true)
        --生成一个礼包道具作为显示
        local bagData = UD:parseRewardToBagItem("1|75000043|1")
        itemIcon:setData(bagData)

        if info.isGet or not canGet then
            local posTemp = itemIcon:convertToWorldSpace(cc.p(0,0))
            local width, height = itemIcon:getContentSize2()
            posTemp.x = posTemp.x + width
            itemIcon:setTipEnable(false)
            itemIcon:setClickHandler(function(render)
                --处于已领取和不能领取的部分点击出现tips
                self:dispatchCellEvent("showHeroTreasureTips", {rewards = info.rewards, pos = posTemp})
            end)
        end
    else
        if #info.rewards == 1 then
            local itemData = info.rewards[1]
            itemIcon:setData(itemData)

            itemIcon:setVisible(true)
            panel:findChild("name"):setVisible(true)
            panel:findChild("name"):setString(itemData:getName(false, 5))
        else
            itemIcon:setVisible(false)
            panel:findChild("icon_over"):setVisible(false)
            panel:findChild("tishi"):setVisible(false)
            panel:findChild("tips"):setVisible(false)
            ani:gotoFrameAndPause(0)
            panel:findChild("name"):setVisible(false)
        end
    end


    if isGolden then
        if canGet then
            --等级够
            panel:findChild("icon_lock"):setVisible(not hasBuyGolden)
            if not hasBuyGolden then
                panel:findChild("tips"):setVisible(false)
                panel:findChild("tishi"):setVisible(false)
            end
        else
            panel:findChild("icon_lock"):setVisible(false)
        end
    end
end

return ClassRef
